package com.ali.edu.web.controller.base;

import java.lang.reflect.ParameterizedType;

public abstract class BaseController<T> {

    protected static String MANAGE_PAGE;    //管理页面
    protected static String INFO_PAGE;      //列表页面
    protected static String EDIT_PAGE;      //编辑页面

    protected static final String MANAGE = "/manage";
    protected static final String INFO = "/info";
    protected static final String EDIT = "/edit";

    //在子类Controller实例化时，(子类)会调用父类的无参构造方法
    public BaseController () {
        //1.获取泛型的真实类型
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
//        System.out.println(type);   //com.ali.edu.web.controller.base.BaseController<com.ali.edu.model.User>
        Class<?> modelClass = (Class<?>) type.getActualTypeArguments()[0];
//        System.out.println(modelClass); //class com.ali.edu.model.User

        //2.获取模块名
        String moduleName = modelClass.getSimpleName().toLowerCase();

        //3.给静态变量赋值
        MANAGE_PAGE = moduleName + MANAGE;
        INFO_PAGE = moduleName + INFO;
        EDIT_PAGE = moduleName + EDIT;
    }
}
